'SMO namespaces
Imports Microsoft.SqlServer.Management.Common
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer
Imports SQLAnalyzer.Framework

Public Class Login
    Private serverConn As ServerConnection

    Public Sub New(ByRef connection As ServerConnection)
        ' Required for Windows Form Designer support
        InitializeComponent()

        serverConn = connection
    End Sub

    Private Sub Login_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cboAuthentication.Items.Add(GlobalConstants.AUTHENTICATION_TYPE_WINDOWS)
        cboAuthentication.Items.Add(GlobalConstants.AUTHENTICATION_TYPE_SQLSERVER)
        cboAuthentication.SelectedIndex = 0
        ProcessWindowsAuthentication()
    End Sub

    Private Sub btnPopulate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPopulate.Click
        Dim csr As Cursor = Nothing
        Dim serverList As DataTable

        Try
            ' Save the old cursor
            csr = Me.Cursor

            ' Display the waiting cursor
            Me.Cursor = Cursors.WaitCursor

            'ToDo: Add busy icon and disable ui
            cboServer.Enabled = False
            ' Set local server as default
            serverList = ServerHelper.GetLocalServerList()

            If serverList.Rows.Count > 0 Then
                cboServer.Items.Clear()
                ' Load server names into combo box
                For Each dr As DataRow In serverList.Rows
                    cboServer.Items.Add(dr("Name"))
                Next

                ' Default to this machine 
                cboServer.SelectedIndex = cboServer.FindStringExact(System.Environment.MachineName)

                ' If this machine is not a SQL server 
                ' then select the first server in the list
                If cboServer.SelectedIndex < 0 Then
                    cboServer.SelectedIndex = 0
                End If
            End If
            cboServer.Enabled = True

        Catch ex As SmoException
            MessageBox.Show(ex.ToString())
        Finally
            ' Restore the original cursor
            Me.Cursor = csr
        End Try
    End Sub

    Private Sub ProcessWindowsAuthentication()
        If cboAuthentication.SelectedIndex = 0 Then
            cboLogin.Enabled = False
            txtPassword.Enabled = False
        Else
            cboLogin.Enabled = True
            txtPassword.Enabled = True
        End If
    End Sub

    Private Sub cboAuthentication_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboAuthentication.SelectedIndexChanged
        ProcessWindowsAuthentication()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        serverConn = Nothing
        Me.Close()
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Dim csr As Cursor = Nothing

        Try
            ' Save the old cursor
            csr = Me.Cursor

            ' Display the waiting cursor
            Me.Cursor = Cursors.WaitCursor

            ' Recreate connection if necessary
            If serverConn Is Nothing Then
                serverConn = New ServerConnection()
            End If

            ' Fill in necessary information
            serverConn.ServerInstance = cboServer.Text.Trim()

            ' Setup capture and execute to be able to display script
            serverConn.SqlExecutionModes = SqlExecutionModes.ExecuteAndCaptureSql

            ' Set connection timeout
            serverConn.ConnectTimeout = 15
            If cboAuthentication.SelectedIndex = 0 Then
                ' Use Windows authentication
                serverConn.LoginSecure = True
            Else
                ' Use SQL Server authentication
                serverConn.LoginSecure = False
                serverConn.Login = cboLogin.Text.Trim()
                serverConn.Password = txtPassword.Text.Trim()
            End If

            ' Go ahead and connect
            serverConn.Connect()
        Catch ex As ConnectionFailureException
            MessageBox.Show(ex.ToString())
        Catch ex As SmoException
            MessageBox.Show(ex.ToString())
        Finally
            ' Restore the original cursor
            Me.Cursor = csr
        End Try
    End Sub
End Class